package cn.yueranzs.login.common;

import cn.yueranzs.system.pojo.Menu;
import cn.yueranzs.system.pojo.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.List;
import java.util.Set;

/**
 * @author yueranzs
 * @date 2021/12/5 12:24
 */
public class UserUtil {


    /**
     * 获取当前登陆人信息
     * @return
     */
    public static User getUser(){
        return (User)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }

    /**
     * 获取当前登录人菜单信息
     * @return
     */
    public static List<Menu> getUserMenu(){
        return getUser().getMenus();
    }
    /**
     * 获取当前登录人账号
     * @return
     */
    public static String getUserName(){
        return getUser().getUsername();
    }
    /**
     * 获取当前登录人编号
     * @return
     */
    public static Long getUserId(){
        return getUser().getId();
    }



    /**
     * 获取当前登录人权限信息
     * @return
     */
    public static Set<? extends GrantedAuthority> getUserRole(){
        return getUser().getAuthorities();
    }

}
